package com.wlanauth.web.util;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.wlanauth.domain.manage.SystemProperty;

/**
 * 计费工具类
 * @author zz
 * @date 2012-5-27
 * @email charles.zz.life@gmail.com
 */
public class RadiusUtils {
	
	/**
	 * 
	 * @param bits 单位M
	 * @param ratio 单位KB/元
	 * @return
	 */
	public static double getFee(float mb,double ratio){
		return mb*1024*ratio;
	}
	public static double getFee(float mb){
		
		SystemProperty prop = SystemPropertyUtils.getProperty(SystemPropertyUtils.KEY_RADIUSRATIO);
		return getFee(mb,Double.parseDouble(prop.getValue()));
	}
	/**
	 * 
	 * @param bit 单位B
	 * @param ratio 单位KB/元
	 * @return
	 */
	public static double getFee(long bit,double ratio){
		return bit/1024*ratio;
	}	
	public static double getFee(long bit){
		SystemProperty prop = SystemPropertyUtils.getProperty(SystemPropertyUtils.KEY_RADIUSRATIO);
		return getFee(bit,Double.parseDouble(prop.getValue()));
	}
	
	public static BigDecimal getViewOct(BigDecimal oct){
		SystemProperty prop = SystemPropertyUtils.getProperty(SystemPropertyUtils.KEY_OCTRULERATIO);
		return oct.multiply(BigDecimal.valueOf(Double.parseDouble(prop.getValue())));
	}
	
	public static void main(String[] args){
		String s = "aaaa<span id=\"span_autoTable_任意\">aaaa\n" +
							"<span>ababa b</span>bbbbb" +
							"<span id=\"span_autoTable_任意\">inner</span>"+
							"<span id=\"span_autoTable_任意\"></span>"+
						"</span>ssssssss" +
						"<span>xxxxxxxxxxxxxxxxxxxxx<span>ppppppppppppppppppppppp</span></span>ttttt<p>ppppppppppppppppppppppp</p>" +
						"<span id=\"span_autoTable_任意\">aaaa2222</span>aaa\ndfdsfdsf<span id=\"span_autoTable_任意\">ttttttttttttttttt</span>2@@2";
		//s = "ssssssssssssssssss<span id=\"span_autoTable_任意\">sdfdsfsssss";
		System.out.println("结果:"+replaceHtmlSymbol("span","id=\"span_autoTable_任意\"",s,""));
	}
	
	public static String replaceHtmlSymbol(String htmlSymbol,String key,String source,String replaceStr){
		//组装特定约束正则
		String keyRegex = "<\\s*"+htmlSymbol+"[^>]?\\s*"+key+"\\s*>(.|\t|\r|\n)*</\\s*"+htmlSymbol+"\\s*>";
		Pattern keyPattern = Pattern.compile(keyRegex);
		Matcher keyMatcher = keyPattern.matcher(source);
		StringBuffer sb = new StringBuffer();
		int start = 0;
		if(keyMatcher.find()){
			int matcherStart = keyMatcher.start();
			String currentGroup = keyMatcher.group();
			String headStr = source.substring(start,matcherStart);
			sb.append(headStr);
			
			//处理后串
			Pattern htmlPattern = Pattern.compile("<\\s*/?\\s*"+htmlSymbol+".*?>");
			Matcher htmlMatcher = htmlPattern.matcher(currentGroup);
			int htmlStartCount = -1,htmlEndCount = -1,keyEnd=currentGroup.length();
			while(htmlMatcher.find()){
				
				if(htmlMatcher.group().matches("<\\s*"+htmlSymbol+".*?>")){
					htmlStartCount++;
				} else {
					htmlEndCount++;
				}
				if(htmlStartCount == htmlEndCount){
					keyEnd = htmlMatcher.end();
					break;
				}
			}
			//替换
			sb.append(String.format(replaceStr,currentGroup.substring(0,keyEnd)));
			sb.append(replaceHtmlSymbol(htmlSymbol,key,source.substring(matcherStart+keyEnd),replaceStr));

		} else {
			return source;
		}
		return sb.toString();
		
	}
}
